package com.adayo.memoryfree.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.adayo.memoryfree.R;
import com.adayo.memoryfree.base.AbsListAdapter;
import com.adayo.memoryfree.bean.AppInfo;
import com.adayo.memoryfree.utils.JStorageKit;
import com.adayo.memoryfree.widgets.RippleView;

import java.util.ArrayList;
import java.util.List;

public class SoftwareAdapter extends AbsListAdapter<AppInfo> {

	public static List<Integer> clearIds;


	public SoftwareAdapter(Context context, List<AppInfo> apps) {
		super(context, apps);
		clearIds = new ArrayList<>();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.listview_software,
					null);
			holder = new ViewHolder();
			holder.appIcon = (ImageView) convertView
					.findViewById(R.id.app_icon);
			holder.appName = (TextView) convertView
					.findViewById(R.id.app_name);
			holder.size = (TextView) convertView
					.findViewById(R.id.app_size);

			holder.ripleUninstall = (RippleView) convertView
					.findViewById(R.id.riple_uninstall);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final AppInfo item = (AppInfo) getItem(position);
		if (item != null) {

			holder.appIcon.setImageDrawable(item.getAppIcon());
			holder.appName.setText(item.getAppName());

			if (item.isInRom()) {
				holder.size.setText(JStorageKit.convertStorage(item.getPkgSize()));
			} else {
				holder.size.setText(JStorageKit.convertStorage(item.getPkgSize()));
			}
		}
		holder.ripleUninstall.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setAction("android.intent.action.VIEW");
				intent.setAction("android.intent.action.DELETE");
				intent.addCategory("android.intent.category.DEFAULT");
				intent.setData(Uri.parse("package:" + item.getPackname()));
				context.startActivity(intent);
			}
		});
		return convertView;
	}


	class ViewHolder {
		ImageView appIcon;
		TextView appName;
		TextView size;
		RippleView ripleUninstall;

		String packageName;
	}
}
